
var netUtil = require('/../../utils/netUtil.js');
var util = require('/../../utils/util.js');
var app = getApp();

Page({
  
  data:{
 			accountPay : null ,  //1:选中缴费记录   2：选中缴费通知单  3：选中欠费单
 			payRecordArray : [],
 			notiRecordArray : [],
 			debtRecordArray : []
  },
  onLoad:function(options){
  	var that = this;
  	app.checkHasRegister(null,function() {
 		 		var toDebt = options.toDebt;
 		 		//有这个标志，就自动选中欠费选项
  			if(toDebt != undefined && toDebt != null && toDebt == 'yes') {
  				that.toPayDebt();
  			} else {
  				that.toPayRecord();
  			}
 			});		
  },
  onReady:function(){
  
  },
  onShow:function(){
    
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
    
    //跳转页面的话，清楚缓存
    this.setData({
    	payRecordArray : [],
 			notiRecordArray : [],
 			debtRecordArray : []
    });
  },

  onUnload:function(){
    // 页面关闭
    
     //跳转页面的话，清楚缓存
		this.setData({
    	payRecordArray : [],
 			notiRecordArray : [],
 			debtRecordArray : []
    });
  },
 //汇总直付
 

  //点击缴费记录
  toPayRecord : function(e) {
  	
  	this.setData({
  		accountPay : 'complete'
  	});
  	
  	app.getUserOpedId(this.getRecord);
  		
  },
  
  toPayNotify : function(e) {
  	this.setData({
  		accountPay : 'Inform'
  	});
  	
  	app.getUserOpedId(this.getRecord);
  },
  
  toPayDebt : function(e) {
  	this.setData({
  		accountPay : 'Arrearage'
  	});
  	
  	app.getUserOpedId(this.getRecord);
  },
	
	//查询记录
	getRecord:function() {
			
		var that = this;

		if(that.data.accountPay == 'complete') {
			
			if(that.data.payRecordArray != null && that.data.payRecordArray.length >0) {
				return;
			}
		
		} else if (that.data.accountPay == 'Inform') {
			
			if(that.data.notiRecordArray != null && that.data.notiRecordArray.length >0) {
				return;
			}
			
		} else if (that.data.accountPay == 'Arrearage') {
			
			if(that.data.debtRecordArray != null && that.data.debtRecordArray.length >0) {
				return;
			}
		
		}
		
		var params = {
  		method : 'wechat.wisen.account.voucher.query',
  		openId : app.globalData.userInfo.openid,
  		data : {
  				accountPay : that.data.accountPay
  			}
  		}
  		netUtil.buildRequest(that,'/wisen/api/wechat/ret',params, {
  			onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				onSuccess : function(resp) {
					
					netUtil.hideLoadingDialog(that);
					
					var data = resp.data;
					
					if(data != undefined && data != null && data.length >= 0) {
					
						if(that.data.accountPay == 'complete') {
							that.setData({
								payRecordArray : data
							});
		
						} else if (that.data.accountPay == 'Inform') {
							that.setData({
								notiRecordArray : data
							});
			
						} else if (that.data.accountPay == 'Arrearage') {
							that.setData({
								debtRecordArray : data
							});
						}		
					}
							
				},
				onError: function(msgCanShow, code, hiddenMsg) {
       	  console.log("fail");
					netUtil.hideLoadingDialog(that);
					netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
				}
  		}).send();
  },
  
  //跳转到记录的详情页
  toDeatilPage : function(e) {
  	var accountPay = this.data.accountPay;
  	var accountPayType =  e.currentTarget.dataset.accountpaytype ;
    var accountCode = e.currentTarget.dataset.accountcode ;
    
    var paramStr = '?accountPay=' + accountPay + '&accountPayType=' + accountPayType + '&accountCode=' + accountCode;
    
    if(accountPay == 'complete') {    //如果是缴费记录
    	 wx.navigateTo({
          url: '/pages/mypay-detail/mypay-detail' + paramStr
      });
    } else if (accountPay == 'Inform') {   //如果是通知单
    	wx.navigateTo({
          url: '/pages/pay-notify-detail/pay-notify-detail' + paramStr
      });
    } else if (accountPay == 'Arrearage') {  //如果是欠费单 	
    	wx.navigateTo({
          url: '/pages/debt-detail/debt-detail' + paramStr
      });
    	
    }
    
  },
  //汇总支付
  countPay: function () {

    var that = this;

    if (that.data.accountPay != 'Arrearage') {
      return;
    } 
    //数据从上页获取
   /*var accountIds='';//单据号拼接
    var accountAccess=0;//入库费用
    var accountStorage=0;//存储费用
    var totalPrice=0;//总价格

    for (var i = 0; i < debtRecordArray.length;i++){
      if(i!=0){
        accountIds += ","+ debtRecordArray[i].accountCode;
      }else{
        accountIds +=  debtRecordArray[i].accountCode;
      }
      if (debtRecordArray[i].accountPayType==1){
        accountStorage += debtRecordArray[i].price;
        totalPrice += debtRecordArray[i].price;
      } else if (debtRecordArray[i].accountPayType == 2||
        debtRecordArray[i].accountPayType == 3){
        accountAccess += debtRecordArray[i].price;
        totalPrice += debtRecordArray[i].price;
      }else{
        netUtil.showAlertDialog("提示", "账单【" + debtRecordArray[i].accountCode+"】对应账单类型不存在，请联系管理员！", false, "确定", null, null);
      }
    }
    var paramStr = '?totalPrice=' + accountPay + '&accountPayType=' + accountPayType + '&accountCode=' + accountCode;*/

   //数据从后台获取
      wx.navigateTo({
        url: '/pages/count-pay-detail/count-pay-detail'
      });
    
  },

	
	
})